package com.qf.controller;

import com.qf.pojo.User;
import com.qf.service.UserService;
import com.qf.service.impl.UserServiceImpl;
import com.qf.utils.Constants;
import com.qf.utils.StrUtils;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet("/user")
public class UserController extends BaseServlet {
    public String login(HttpServletRequest request, HttpServletResponse response) {
//      获取用户参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String code = request.getParameter("code");
//        获取用户自动登录的参数
        String auto = request.getParameter("auto");
//      判断用户名   密码  验证码 不为空   如果为空重定向到登录页面
       if(!StrUtils.empty(username) ||!StrUtils.empty(password)||!StrUtils.empty(code)){
            request.setAttribute("msg", "用户名，密码，验证码不能为空");
            return "forward:/login.jsp";
        }
//      校验验证码是是否正确  不正确重定向到登录页面重新登录
        String sessionCode =(String) request.getSession().getAttribute("code");
       if (!code.equalsIgnoreCase(sessionCode)){
           request.setAttribute("username", username);
           request.setAttribute("password", password);
           request.setAttribute("smg", "验证码错误");
           return "forward:/login.jsp";
       }
//      调用service完成用户名密码的校验
        UserService userService = new UserServiceImpl();
        User user = (userService).login(username, password);
//      判断用户名 密码是否错误   如果错误返回提示信息
        if(user == null){
            request.setAttribute("username", username);
            request.setAttribute("password", password);
            request.setAttribute("smg", "用户名或密码错误");
            return "forward:/login.jsp";
        }
//        登录成功的话，将用户信息放入tomcat的session会话中携带
        request.getSession().setAttribute("loginUser", user);
//        判断用户是否要自动登录   进行自动登录设置
        if ("on".equals(auto)){
            Cookie cookie = new Cookie(Constants.AUTO_NAME, user.getUsername());
            //设置这个cookie保存的路径
            cookie.setPath("/");
            //设置cookie保存数据的时间
            cookie.setMaxAge(60*60*24*14);
            //通过响应将cookie数据写会浏览器中保存
            response.addCookie(cookie);
        }
        return "redirect:/goods?method=index";
    }
        public String register(HttpServletRequest request,HttpServletResponse response) throws InvocationTargetException, IllegalAccessException {
            Map<String, String[]> parameterMap = request.getParameterMap();

            User user = new User();
            BeanUtils.populate(user, parameterMap);
            UserService userService = new UserServiceImpl();
            int i = userService.addUser(user);
            if (i >0){
                return "redirect:/registerSuccess.jsp";
            }else {
                return "rdirect:/register.jsp";
            }
        }

}
